Converted these images to PNG, saving a handful of bytes per image
[adiumx.git] / Frameworks / FriBidi Framework / NSString-FBAdditions.m
blob7732d61fb7ad8f7d70d2cd10410d0149248b5b16
1 //
2 //  NSString-FBAdditions.m
3 //  FriBidi
4 //
5 //  Created by Ofri Wolfus on 22/08/06.
6 //  Copyright 2006 Ofri Wolfus. All rights reserved.
7 //
9 #import "NSString-FBAdditions.h"
10 #import "fribidi.h"
11 #import "ConvertUTF.h"
14 @implementation NSString (FBAdditions)
17  * This method attempts to determine the base writing direction of our string.
18  * We do this by looping through our characters until we find the first one that knows its writing direction.
19  * This method will usually get the right direction, unless someone accidentally put a random letter at the
20  * beginning of the string, which has a different writing direction.
21  * But since this will be the user's fault, he'll have to deal with it :)
22  * Anyhow, AppKit also uses this method when displaying bidi text.
23  */
24 - (NSWritingDirection)baseWritingDirection {
25         unsigned int            len = [self length];
26         unsigned int            i;
27         FriBidiChar                     *f, fch;
28         UTF16                           *u, uch;
29         NSWritingDirection      dir = NSWritingDirectionNatural;
30         
31         /*
32          * Loop through all our characters, one by one, until we find one which knows its writing direction.
33          * Note: If our string begins with lots of universal characters (characters without a direction), this
34          * could get very inefficient.
35          */
36         for (i = 0U; i < len; i++) {
37                 FriBidiCharType type;
38                 
39                 // Get a single character
40                 uch = (UTF16)CFStringGetCharacterAtIndex((CFStringRef)self, i);
41                 u = &uch;
42                 f = &fch;
43                 
44                 // Convert our UniChar (which is UTF16) to FriBidiChar (which is UTF32)
45                 if (ConvertUTF16toUTF32((const UTF16**)&u, (u + 1), &f, (f + 1), lenientConversion) == conversionOK) {
46                         // Get the type of our character
47                         type = fribidi_get_type(fch);
48                         
49                         // LTR char?
50                         if (type == FRIBIDI_TYPE_LTR) {
51                                 dir = NSWritingDirectionLeftToRight;
52                                 break;
53                         }
54                         
55                         // RTL char?
56                         if (type == FRIBIDI_TYPE_RTL) {
57                                 dir = NSWritingDirectionRightToLeft;
58                                 break;
59                         }
60                 }
61         }
62         
63         return dir;
66 @end